package com.juyu.reader.pc.util;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author huangboning
 * 
 */
public class StringUtil {

	public static boolean isNullOrEmpty(String str) {
		if (null == str) {
			return true;
		}
		if ("".equals(str)) {
			return true;
		}
		return false;
	}

	public static String createValidateCode() {
		int randomInt = new Random().nextInt(999999);
		String randomString = String.valueOf(randomInt);
		return randomString;
	}

	public static String createUserNo() {
		int randomInt = (int) ((Math.random() * 9 + 1) * 100000000);
		String randomString = String.valueOf(randomInt);
		return randomString;
	}

	public static String createUserAppNo() {
		int randomInt = new Random().nextInt(999999999);
		String randomString = String.valueOf(randomInt);
		String str = "a" + randomString;
		return str;
	}

	public static String createUserNickname(String code) {
		int randomInt = new Random().nextInt(999999);
		String randomString = String.valueOf(randomInt);
		String str = "s" + code + randomString;
		return str;
	}

	public static String createUserNicknameWX() {
		int randomInt = new Random().nextInt(99999999);
		String randomString = String.valueOf(randomInt);
		String str = "wx" + randomString;
		return str;
	}

	public static String createUserNicknameWB() {
		int randomInt = new Random().nextInt(99999999);
		String randomString = String.valueOf(randomInt);
		String str = "wb" + randomString;
		return str;
	}

	public static String createUserNicknameQQ() {
		int randomInt = new Random().nextInt(99999999);
		String randomString = String.valueOf(randomInt);
		String str = "qs" + randomString;
		return str;
	}

	public static String createOrderNo(String code) {
		int randomInt = new Random().nextInt(9999);
		String randomString = String.valueOf(randomInt);
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String dateString = dateFormater.format(Calendar.getInstance().getTime());
		randomString = code + dateString + randomString;
		return randomString;
	}

	public static String getNowTime() {
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = dateFormater.format(Calendar.getInstance().getTime());
		return dateString;
	}

	public static String getTimestamp() {
		long times = Calendar.getInstance().getTime().getTime() / 1000;
		return String.valueOf(times);
	}

	public static long getLongTimestamp(Date date) {
		if (date == null) {
			return 0;
		}
		long times = date.getTime() / 1000;
		return times;
	}

	public static String createNumberUUID() {
		String numberUUID = java.util.UUID.randomUUID().toString();
		numberUUID = numberUUID.substring(0, 8) + numberUUID.substring(9, 13) + numberUUID.substring(14, 18)
				+ numberUUID.substring(19, 23) + numberUUID.substring(24);
		return numberUUID;

	}

	public static String createSecretKeyId() {
		int randomInt = new Random().nextInt(99999999);
		String randomString = String.valueOf(randomInt);
		randomString = Md5Util.md5Encode(randomString);
		String str = new String(Base64.getEncoder().encode(randomString.getBytes()));
		randomString = str.substring(0, 8);
		return randomString;
	}

	public static String createSecretKeyValue() {

		String numberUUID = java.util.UUID.randomUUID().toString();
		numberUUID = numberUUID.substring(0, 8) + numberUUID.substring(9, 13) + numberUUID.substring(14, 18)
				+ numberUUID.substring(19, 23) + numberUUID.substring(24);
		numberUUID = Md5Util.md5Encode(numberUUID);
		String str = new String(Base64.getEncoder().encode(numberUUID.getBytes()));
		numberUUID = str.substring(0, 18);
		return numberUUID;
	}

	/**
	 * unicode解码（unicode编码转中文）
	 *
	 * @param theString
	 * @return
	 */
	public static String unicodeDecode(String theString) {
		char aChar;
		int len = theString.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for (int x = 0; x < len;) {
			aChar = theString.charAt(x++);
			if (aChar == '\\') {
				aChar = theString.charAt(x++);

				if (aChar == 'u') {
					// Read the xxxx
					int value = 0;
					for (int i = 0; i < 4; i++) {
						aChar = theString.charAt(x++);
						switch (aChar) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
						}
					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';
					else if (aChar == 'n')
						aChar = '\n';
					else if (aChar == 'f')
						aChar = '\f';
					outBuffer.append(aChar);
				}
			} else
				outBuffer.append(aChar);
		}
		return outBuffer.toString();
	}

	public static String delHTMLTag(String htmlStr) {
		String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
		String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
		String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

		Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签
		htmlStr = htmlStr.replace(" ", "");// 去掉空格
		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * 四舍五入保留两位小数点
	 * 
	 * @param f
	 * @return
	 */
	public static String getFloatString2(float f) {

		NumberFormat nf = NumberFormat.getInstance();
		nf.setRoundingMode(RoundingMode.HALF_UP);// 设置四舍五入
		nf.setMinimumFractionDigits(2);// 设置最小保留几位小数
		nf.setMaximumFractionDigits(2);// 设置最大保留几位小数
		return nf.format(f);
	}

	/**
	 * 四舍五入保留两位小数点
	 * 
	 * @param f
	 * @return
	 */
	public static float getFloat2(float f) {

		NumberFormat nf = NumberFormat.getInstance();
		nf.setRoundingMode(RoundingMode.HALF_UP);// 设置四舍五入
		nf.setMinimumFractionDigits(2);// 设置最小保留几位小数
		nf.setMaximumFractionDigits(2);// 设置最大保留几位小数
		return Float.valueOf(nf.format(f));
	}

	/**
	 * 表单类型报文解析成数据映射表
	 * 
	 * @param reportContent
	 * @return
	 */
	public static Map<String, String> parseFormDataPatternReport(String reportContent) {
		if (reportContent == null || reportContent.length() == 0)
			return null;

		String[] domainArray = reportContent.split("&");

		Map<String, String> key_value_map = new HashMap<String, String>();
		for (String domain : domainArray) {
			String[] kvArray = domain.split("=");

			if (kvArray.length == 2) {
				try {
					key_value_map.put(kvArray[0], kvArray[1]);
				} catch (Exception ex) {
					// ignore
				}

			}
		}

		return key_value_map;
	}
}
